﻿using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProudMonkey.WebTools
{
    public class SiteNavigationTrail: WebControl
    {
        public SiteNavigationTrail() { }

        internal string _groupName = string.Empty;
        private Stack<LinkNode> URLList {
            get {
                if (HttpContext.Current.Session["GRPKEY_" + _groupName] == null) {
                    HttpContext.Current.Session["GRPKEY_" + _groupName] = new Stack<LinkNode>();
                }
                return (Stack<LinkNode>)HttpContext.Current.Session["GRPKEY_" + _groupName];
            }
            set {
                HttpContext.Current.Session["GRPKEY_" + _groupName] = value;
            }
        }
        internal void AddNode(LinkNode node) {
            LinkNode[] nodes = URLList.ToArray();
            bool nodeExists = false;

            for (int i = 0; i < nodes.Length; i++) {
                if (node.URLName.Equals(nodes[i].URLName))
                    nodeExists = true;
            }

            if (nodeExists) {
                LinkNode tempNode = null;
                do {
                    tempNode = URLList.Pop();
                }
                while (!tempNode.URLName.Equals(node.URLName));
            }

            URLList.Push(node);
        }
        internal void AddNode(string name, string path) {
            AddNode(new LinkNode(name, path));
        }
        internal void RootLevel() {
            URLList.Clear();
        }
        internal void FirstLevel() {
            LinkNode[] nodes = URLList.ToArray();
            if (nodes.Length > 0) {
                URLList.Clear();
                URLList.Push(nodes[nodes.Length - 1]);
            }
        }
        internal void SecondLevel() {
            LinkNode[] nodes = URLList.ToArray();
            if (nodes.Length > 1) {
                URLList.Clear();
                URLList.Push(nodes[nodes.Length - 1]);
                URLList.Push(nodes[nodes.Length - 2]);
            }
        }
        internal void ThirdLevel() {
            LinkNode[] nodes = URLList.ToArray();
            if (nodes.Length > 2) {
                URLList.Clear();
                URLList.Push(nodes[nodes.Length - 1]);
                URLList.Push(nodes[nodes.Length - 2]);
                URLList.Push(nodes[nodes.Length - 3]);
            }
        }
        protected override void Render(System.Web.UI.HtmlTextWriter writer) {
            base.Render(writer);
            LinkNode[] nodes = URLList.ToArray();
             if (nodes.Length > 0) {
                 for (int i = nodes.Length - 1; i > 0; i--) {
                     writer.Write("<a href=\"");
                     writer.Write(nodes[i].URLPath);
                     writer.Write("\">");
                     writer.Write(nodes[i].URLName);
                     writer.Write("</a>");
                     writer.Write(" / ");
                 }

                 writer.Write("<b>");
                 writer.Write(nodes[0].URLName);
                 writer.Write("</b>");
             }
        }
    }

    internal class LinkNode
    {
        private string _urlName = string.Empty;
        private string _urlPath = string.Empty;
        private string _groupName = string.Empty;

        public LinkNode(string name, string path) {
            _urlName = name;
            _urlPath = path;
        }
        public string URLName {
            get {
                return _urlName;
            }
        }
        public string URLPath {
            get {
                return _urlPath;
            }
        }
    }

    public class Trail
    {
        public enum Level
        {
            Root = 0,
            One = 1,
            Two = 2,
            Three =3,
            NoLevel
        }
        private static Control FindControlRecursive(Control root, string Id) {
            if (root.ID == Id)
                return root;
            foreach (Control control in root.Controls) {
                Control foundControl = FindControlRecursive(control, Id);
                if (foundControl != null)
                    return foundControl;
            }
            return null;
        }
        public static void AddTrail(System.Web.UI.Control breadCrumbContainer, string breadCrumbControlID, string groupName, string nodeName, Level nodeLevel) {
            SiteNavigationTrail crumb = (SiteNavigationTrail)FindControlRecursive(breadCrumbContainer, breadCrumbControlID);
            crumb._groupName = groupName;
            switch (nodeLevel) {
                case Level.Root:
                    crumb.RootLevel();
                    crumb.AddNode(nodeName, HttpContext.Current.Request.Url.AbsoluteUri);
                    break;
                case Level.One:
                    crumb.FirstLevel();
                    crumb.AddNode(nodeName, HttpContext.Current.Request.Url.AbsoluteUri);
                    break;
                case Level.Two:
                    crumb.SecondLevel();
                    crumb.AddNode(nodeName, HttpContext.Current.Request.Url.AbsoluteUri);
                    break;
                case Level.Three:
                    crumb.ThirdLevel();
                    crumb.AddNode(nodeName, HttpContext.Current.Request.Url.AbsoluteUri);
                    break;
                case Level.NoLevel:
                    crumb.AddNode(nodeName, HttpContext.Current.Request.Url.AbsoluteUri);
                    break;
            }
        }
    }
}
